﻿using MS.Entities;
using MS.Enums.Common;
using MS.Utility.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace MS.Logic
{
    public partial class MS_Base_FunctionLogic
    {
        /// <summary>
        /// 保存菜单或者功能
        /// </summary>
        /// <param name="id">id为0代变新增，有值代变编辑</param>
        /// <param name="pid">上级id</param>
        /// <param name="name">名称</param>
        /// <param name="url">url地址</param>
        /// <param name="type">类型 对应枚举FunctionType </param>
        /// <param name="iconClassName">图标类名称</param>
        /// <returns></returns>
        public int Save(int id, int pid, string name, string url, FunctionType type, string iconClassName, long uid)
        {
            if (id == 0)
            {
                //新增
                MS_Base_Function function = new MS_Base_Function();
                function.Pid = pid;
                function.Name = name.Trim();
                function.Url = url.Trim();
                function.Type = (int)type;
                function.IconClassName = iconClassName.Trim();
                function.CreateTime = DateTimeHelper.GetTime(DateTime.Now);
                function.CreateUid = uid;
                function.UpdateTime = DateTimeHelper.GetTime(DateTime.Now);
                function.UpdateUid = uid;

                int level = 0;
                string path = "/";
                int seqid = 1;

                int count = this.GetCountByQuery(x => x.Pid == pid);
                seqid = count + 1;

                MS_Base_Function parent = this.GetItemByQuery(x => x.Id == pid);
                if (parent != null)
                {
                    level = parent.Level + 1;
                    path = parent.Path + parent.Id + "/";
                }
                function.Level = level;
                function.Path = path;
                function.SeqId = seqid;
                this.Add(function);
                id = function.Id;

            }
            else
            {
                //编辑
                MS_Base_Function function = this.GetById(id);
                if (function != null)
                {
                    function.Name = name;
                    function.Url = url;
                    function.Type = (int)type;
                    function.IconClassName = iconClassName;
                    function.UpdateTime = DateTimeHelper.GetTime(DateTime.Now);
                    function.UpdateUid = uid;
                    this.Save(function);
                }
            }
            return id;
        }

        /// <summary>
        /// 移动菜单或功能
        /// </summary>
        /// <param name="id">菜单或功能id</param>
        /// <param name="way">移动的方向</param>
        /// <param name="uid">操作人id</param>
        /// <returns></returns>
        public bool Move(int id, MoveWay way, long uid)
        {
            bool b = true;
            MS_Base_Function function = this.GetItemByQuery(x => x.Id == id && x.Status == (int)Status.Normal);
            if (function == null)
            {
                return false;
            }
            List<MS_Base_Function> list = null;
            MS_Base_Function tmpFunction = null;
            switch (way)
            {
                case MoveWay.Up:
                    //向上移动
                    //查询同类集合
                    list = this.GetListByQuery(x => x.Status == (int)Status.Normal && x.Pid == function.Pid, "SeqId desc", false);
                    tmpFunction = list.Find(x => x.SeqId < function.SeqId);
                    break;
                case MoveWay.Down:
                    //向下移动
                    //查询同类集合
                    list = this.GetListByQuery(x => x.Status == (int)Status.Normal && x.Pid == function.Pid, "SeqId", false);
                    tmpFunction = list.Find(x => x.SeqId > function.SeqId);
                    break;
                default:
                    break;
            }
            if (tmpFunction != null)
            {
                int tmpSeqId = tmpFunction.SeqId;
                tmpFunction.SeqId = function.SeqId;
                tmpFunction.UpdateUid = uid;
                tmpFunction.UpdateTime = DateTimeHelper.GetTime(DateTime.Now);

                function.SeqId = tmpSeqId;
                function.UpdateUid = uid;
                function.UpdateTime = DateTimeHelper.GetTime(DateTime.Now);
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {
                    try
                    {
                        this.Save(tmpFunction);
                        this.Save(function);
                        scope.Complete();
                    }
                    catch
                    {
                        throw;
                    }
                }

            }
            else
            {
                b = false;
            }
            return b;
        }
    }
}
